<template>
  <div>
    <el-dialog :title="info.isAdd ?'管理员添加':'管理员修改'" :visible.sync="info.isshow" @close="close">
      user:{{user}}
      <el-form :model="user">
        <el-form-item label="所属角色" label-width="120px">
          <el-select v-model="user.roleid">
            <el-option v-for="item in roleList" :key="item.id" :label="item.rolename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="user.password" autocomplete="off" show-password></el-input>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add()">添加</el-button>
        <el-button type="primary" v-else @click="update">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { IndexRoutes } from "../../../router/index";
import { successAlert } from "../../../utils/alert.js";
// import {reqmenuadd,requserinfo} from "../../../http/http.js"
import {requserinfo} from '../../../http/api';
import {
  reqroleadd,
  reqmenuedit,
  reqmenuinfo,
  reqrolelist,
  reqmenulist,
  reqroleinfo,
  requseredit,
  requseradd
} from "../../../http/api";
import { formClose } from "../../../mixins";

export default {
  //接受info
  props: ["info", "arr"],
  mixins: [formClose],
  data() {
    return {
      //初始化表单数据
      user: {
        roleid: "",
        username: "",
        password: "",
        status:1
      },
      //路由集合
      IndexRoutes,
      //角色的列表
      roleList:[]
    };
  },
  computed: {
    ...mapGetters({
    userInfo:"userInfo"
    })
  },
  mounted() {
      //请求角色的列表
      reqrolelist().then(res=>{
        if(res.data.code ==200){
          this.roleList=res.data.list?res.data.list:[]
        }
      })
  },
  methods: {
    ...mapActions({
      changeUserInfo:"changeUserInfo"
    }),

    //清空user
    empty() {
      this.user = {
        roleid: "",
        username: "",
        password: "",
        status:1
      };
    },
    //点了添加
    add() {
      requseradd(this.user).then(res => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          //弹框清空
          this.empty();
          //弹框消失
          this.info.isshow = false;
          //重新请求列表
          this.$emit("init");
        }
      });
    },
    //上级菜单发生了改变
    changepid() {
      //如果上级菜单是顶级菜单，那么类型就是目录。否则就是菜单，看pid
      if (this.user.pid == 0) {
        this.user.type = 1;
      } else {
        this.user.type = 2;
      }
    },
    //获取一条数据
    getOne(id) {
      requserinfo({ uid: id }).then(res => {
        if (res.data.code == 200) {
          this.user = res.data.list;
          //处理密码
          this.user.password=""
        }
      });
    },
    // 修改
    update() {
      requseredit(this.user).then(res => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          //如果user.uid ==userInfo.uid,清空用户数据，并跳转到登陆
           if(this.user.uid===this.userInfo.uid){
             this.changeUserInfo({})
             this.$router.push("/login")
             return 
           } 
          //弹框清空
          this.empty();
          //弹框消失
          this.info.isshow = false;
          //重新请求列表
          this.$emit("init");
        }
      });
    }
  }
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>